package com.scsj.supermarket.view.fragment.couponmodel;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.CouponAdapter;
import com.scsj.supermarket.base.QueryConpouBase;
import com.scsj.supermarket.bean.CoupouRequestBean;
import com.scsj.supermarket.bean.MineCouponBean;
import com.scsj.supermarket.config.RxSPConfig;
import com.scsj.supermarket.customview.DecorationSpacesVertical;
import com.scsj.supermarket.presenter.MineConpouPresenter;
import com.scsj.supermarket.view.fragment.basemodel.BaseFragment;
import com.vondear.rxtools.RxImageTool;
import com.vondear.rxtools.RxSPTool;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;
import okhttp3.RequestBody;

/**
 *  优惠券 - 未使用
 */
public class CouponUnused extends BaseFragment implements QueryConpouBase.IView {

    private RecyclerView recyclerview;
    private CouponAdapter couponAdapter;
    private MineConpouPresenter presenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_coupon_unused, null);
        presenter = new MineConpouPresenter(this);
        initView(view, null);
        initListener();
        initData();
        return view;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        recyclerview = view.findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(getHoldingActivity()));
        recyclerview.addItemDecoration(new DecorationSpacesVertical(RxImageTool.dip2px(10),RxImageTool.dip2px(15)));
    }



    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

        CoupouRequestBean.PageInfoBean bean = new CoupouRequestBean.PageInfoBean();
        bean.setPageNum(1);
        bean.setPageSize(20);
//        state: 0未使用;1已使用;2已失效
        String userID = RxSPTool.getString(getHoldingActivity(), RxSPConfig.USER_ID);
        CoupouRequestBean requestBean = new CoupouRequestBean(bean,"0",userID);
        Gson gson = new Gson();
        String obj = gson.toJson(requestBean);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), obj);
        presenter.deliverData(body);
    }

    @Override
    protected int getLayoutId() {
        return 0;
    }


    @Override
    public void showSuccessMsg(String s, MineCouponBean mineCouponBean) {
        List<MineCouponBean.DataBean.ListBean> list = mineCouponBean.getData().getList();
        couponAdapter = new CouponAdapter(getHoldingActivity(),list,0);
        recyclerview.setAdapter(couponAdapter);
    }

    @Override
    public void showErrorMsg(String s) {
        Toasty.error(getHoldingActivity(),s).show();
    }

}
